package mysql

import (
	"gorm.io/gorm"
	"video-joy/userrpc/userrpc"
)

type CommentInfo struct {
	gorm.Model
	VideoId        int64  `gorm:"type:int(11);not null"`
	UserId         int64  `gorm:"type:int(11);not null"`
	CommentContent string `gorm:"type:varchar(500);not null"`
	IsAnonymous    int64  `gorm:"type:int(11);not null"`
	CommentStatus  int64  `gorm:"type:int(11);not null"`
	ReplyId        int64  `gorm:"type:int(11);"`
}

func (i *CommentInfo) CommentAdd(db *gorm.DB) error {
	return db.Debug().Create(i).Error
}

func (i *CommentInfo) CommentDel(db *gorm.DB) error {
	return db.Debug().Delete(i).Error
}

func (i *CommentInfo) CommentById(db *gorm.DB, id int64) error {
	return db.Debug().Where("comment_infos.id", id).First(i).Error
}

/*
SELECT
  comment_infos.*,
  user_infos.nick_name,
  comment_infos.comment_content,
  comment_infos.created_at
FROM
  comment_infos
  LEFT JOIN user_infos ON comment_infos.user_id = user_infos.id
  LEFT JOIN video_infos ON comment_infos.video_id=video_infos.id
*/

func CommentList(db *gorm.DB, in *userrpc.CommentListRequest) []*userrpc.CommentList {
	var commentlist []*userrpc.CommentList

	db.Debug().Model(&CommentInfo{}).
		Select(
			"comment_infos.*",
			"comment_infos.id as comment_id",
			"user_infos.nick_name",
			"comment_infos.comment_content",
			"comment_infos.created_at").
		Joins("LEFT JOIN user_infos ON comment_infos.user_id = user_infos.id").
		Joins("LEFT JOIN video_infos ON comment_infos.video_id=video_infos.id").
		Order("comment_infos.created_at desc").
		Scopes(Paginate(int(in.Page), int(in.PageSize))).
		Find(&commentlist)

	return commentlist
}
